<?php

/**
 * 前台 用户 模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: news.php 39 2011-07-11 17:12:35Z zwt007 $
 * http://www.t-mac.org；
 */
class userAction extends Action
{

    private $tmp_model;
    private $show_model;
    private $index_model;
    private $check_model;

    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('user');
        $this->show_model = Tmac::model('show');
        $this->index_model = Tmac::model('index');
        $this->M('index')->getCommon();
        $this->check_model = $this->M('check');
    }

    public function index()
    {
        $this->check_model->CheckPurview('tb_user,tb_admin');
        //取博客列表
        $this->tmp_model->getPostList();
        $this->V('user_bloglist');
    }

    public function addblog()
    {
        $this->check_model->CheckPurview('tb_user,tb_admin');
        $id = intval($this->getParam('id'));
        if ($id > 0) {
            $editinfo = $this->tmp_model->getPostInfo($id);
        } else {
            $editinfo = array('id' => '', 'title' => '', 'orderby' => '', 'content' => '');
        }
        $this->assign('info', $editinfo);
        $this->V('user_addblog');
    }

    public function blogsave()
    {
        $this->check_model->CheckPurview('tb_user,tb_admin');
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $id = intval($this->getParam('id'));
        $uid = $_SESSION['uid'];
        $username = $_SESSION['username'];
        $title = trim($this->H($this->getParam('title')));
        $orderby = intval($this->getParam('orderby'));
        $content = $this->getParam('content');

        //判断是否为空
        if (empty($title)) {
            $this->redirect('标题不能力空');
            exit;
        }

        if (empty($content)) {
            $this->redirect('内容不能力空');
            exit;
        }

        $info = array(
            'title' => $title,
            'uid' => $uid,
            'username' => $username,
            'orderby' => $orderby,
            'content' => $content,
            'post_time' => time()
        );
        if (!empty($id)) {
            unset($info['post_time']);
            $info['edit_time'] = time();
        }

        if ($id > 0) {
            $this->tmp_model->checkUser($id);
            $blog_page = HttpResponse::getCookie('blog_page');
            //update save article
            $where = 'id = ' . $id . '';
            $rs = $this->tmp_model->addPost($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改博文成功', PHP_SELF . '?m=user&page= ' . $blog_page . '');
            } else {
                $this->redirect('修改博文失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addPost($info);
            if ($rs) {
                $this->redirect('发布博文成功');
            } else {
                $this->redirect('发布博文失败，请联系技术支持检查原因！');
            }
        }
    }

    public function blogshow()
    {
        $id = intval($this->getParam('id'));
        if (empty($id)) {
            $this->redirect('请选择博文！');
            exit();
        }

        $info = $this->tmp_model->getPostInfo($id);

        $this->assign('info', $info);
        $this->V('blog_show');
    }

    /**
     * del
     * @param int $class_id
     */
    public function delblog()
    {
        $this->check_model->CheckPurview('tb_user,tb_admin');
        $id = intval($this->getParam('id'));
        $id_a = $this->getParam('id_a');
        //todo 判断权限
        $this->tmp_model->checkUser($id);

        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($id)) {
            $id = $id;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delPost($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $this->redirect('删除博客成功');
        } else {
            $this->redirect('删除博客失败，请重试！');
        }
    }

    public function reg()
    {

        $this->V('user_reg');
    }

    public function save()
    {
        $username = trim($this->H($this->getParam('username')));
        $password = $this->H($this->getParam('password'));
        $password1 = $this->H($this->getParam('password1'));
        $check_username = $this->tmp_model->checkUserName('username', $username, '');

        if ($check_username) {
            $this->redirect('用户名已经存在啊！');
            exit();
        }
        $question = trim($this->H($this->getParam('question')));
        $answer = trim($this->H($this->getParam('answer')));

        //做验证
        if (empty($username)) {
            $this->redirect('用户名不能为空啊！');
            exit();
        }
        if (empty($password) || empty($password1)) {
            $this->redirect('密码不能为空啊！');
            exit();
        }
        if (strlen($password) < 6 || strlen($password1) < 6) {
            $this->redirect('密码不能小于６位数啊！');
            exit();
        }
        if ($password != $password1) {
            $this->redirect('二次密码不相同啊！');
            exit();
        }
        if (empty($question)) {
            $this->redirect('请填写密码保护问题！');
            exit();
        }
        if (empty($answer)) {
            $this->redirect('请填写密码保护答案！');
            exit();
        }
        $url = array('question' => $question, 'answer' => $answer);
        $url = serialize($url);

        $info = array(
            'username' => $username,
            'password' => md5(md5($password)),
            'url' => $url,
            'rank' => 2,
            'reg_time' => date('Y-m-d', time())
        );

        //insert save article_class
        $rs = $this->tmp_model->addUser($info);
        if ($rs) {
            //注册session
            $_SESSION['username'] = $info['username'];
            $_SESSION['uid'] = $rs;
            $_SESSION['user_purviews'] = 'txh_user';
            $this->redirect('注册成功', PHP_SELF . '?m=user');
        } else {
            $this->redirect('注册失败');
        }
    }

}